<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class UserController extends CI_Controller {
	
	function __construct() {
		parent::__construct ();
		$this->load->model ( 'user' );
	}
	
	function index() {
		//Field validation succeeded.  Validate against database
		// $username = $this->input->post('username');
		$username = $_POST ['username'];
		$password = $_POST ['password'];
		/*$username = "binh";
		$password = "111";*/
		
		//query the database
		$result = $this->user->login ( $username, $password );
		
		if ($result) {
			/*$sess_array = array ();
			foreach ( $result as $row ) {
				$sess_array = array ('id' => $row->id_User, 'username' => $row->username );
				$this->session->set_userdata ( 'logged_in', $sess_array );
			}*/
			//print ("1") ;
			print (json_encode ( $result )) ;
		} else {
			//$this->form_validation->set_message ( 'check_database', 'Invalid username or password' );
			//print ("0") ;
			print "0";
		}
	}
	function getUserName() {
		$id_User = $_POST ['id_User'];
		$result = $this->user->getUserName ( $id_User );
		print (json_encode ( $result )) ;
	}
	function addUser() {
		$username = $_POST ['username'];
		$password = $_POST ['password'];
		$email = $_POST ['email'];
		$check = $this->user->getIdUser ( $username );
		if ($check) //ton tai tk nay
			print ('0') ;
		else { //neu chua thi add
			$id_User = $this->user->addUser ( $username, $password, $email );
			print $id_User;
		}
	
	}
}
?>